package com.supinfo.suptracking.servlet;

import com.supinfo.suptracking.entity.Car;
import com.supinfo.suptracking.entity.Zone;
import com.supinfo.suptracking.service.CarService;
import com.supinfo.suptracking.service.ZoneService;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Fabien, Jérémy, Ludovic, Brice
 */

@WebServlet(name = "EditCarServlet", urlPatterns = {"/auth/editCar"})
public class EditCarServlet extends HttpServlet {

    @EJB
    private CarService carServ;
    
    @EJB
    private ZoneService zoneService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        Date date = null;
        // Get form fields
        String safeZoneIdTemp= request.getParameter("safeZone");
        String carIdTemp = request.getParameter("carIdEdit");
        String carName = request.getParameter("carNameEdit");
        String carBrand = request.getParameter("carBrandEdit");
        String carInitialEntryIntoService = request.getParameter("carInitialEntryIntoServiceEdit");
        
        // Transforme string in date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = sdf.parse(carInitialEntryIntoService);
        } catch (ParseException ex) {
            Logger.getLogger(AddCarServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        // Transform string in long
        Long carId=Long.valueOf(carIdTemp);
        Long safeZoneId=Long.valueOf(safeZoneIdTemp);
        // Get car and zone and update it
        Car caredited = carServ.processGetCarById(carId);
        Zone safeZone= zoneService.processGetZoneById(safeZoneId);
        
        caredited.setBrand(carBrand);
        caredited.setName(carName);
        caredited.setInitialEntryIntoService(date);
        if(safeZone.getCar()==null){
            safeZone.setCar(caredited);
            zoneService.processUpdateZone(safeZone);}
        else{
            request.getSession().setAttribute("event", "0");
            request.getSession().setAttribute("message", "Car not modified! Zone already attribued to a car");
            response.sendRedirect(getServletContext().getContextPath()+"/auth/listCar");
        }
        
        // Update in base
        carServ.processUpdateCar(caredited);

        
        // Notification of user and redirection
        request.getSession().setAttribute("event", "1");
        request.getSession().setAttribute("message", "Car modified");
        response.sendRedirect(getServletContext().getContextPath()+"/auth/listCar");	
    }
}
